<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>生成器函数实例-2</title>
</head>
<body>
  <script>
    //模拟获取  用户数据  订单数据  商品数据
    function getUsers () {
      setTimeout(() => {
        let data = '用户数据'
        //调用 next 方法, 并且将数据传入
        iterator.next(data)
      }, 1000)
    }

    function getOrders () {
      setTimeout(() => {
        let data = '订单数据'
        iterator.next(data)
      }, 1000)
    }

    function getGoods () {
      setTimeout(() => {
        let data = '商品数据'
        iterator.next(data)
      }, 1000)
    }

    function * gen () {
      let users = yield getUsers()
      console.log(users)
      let orders = yield getOrders()
      console.log(orders)
      let goods = yield getGoods()
      console.log(goods)
    }

    //调用生成器函数
    let iterator = gen()
    iterator.next()
  </script>
</body>
</html>